package com.glsc.ngateway.platform.repo;

import com.glsc.ngateway.common.api.flowable.dto.FlowParamAuthDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.VEhrAdminStaff;
import com.glsc.ngateway.platform.dto.FlowParamDevManageSearchDto;
import com.glsc.ngateway.platform.domain.FlowFormDevManage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface DevManageRepository extends JpaRepository<FlowFormDevManage, Long>, JpaSpecificationExecutor<FlowFormDevManage> {
    FlowFormDevManage findByProcessId(String processId);


    @Query(value = "select u from FlowFormDevManage u where u.isClose = 0 and u.isDelete = 0")
    List<FlowFormDevManage> queryAllByNODelAndNOCloase();
    List<FlowFormDevManage> findAllByIsDelete(Integer noDelete);

    @Query(value = "select * from t_gw_flow_form_dev_manage p where p.is_delete = 0 " +
            "and (:#{#paramSearchDto.processId} is null OR p.process_id = :#{#paramSearchDto.processId}) " +
            "and (:#{#paramSearchDto.processCreateTime} is null OR p.process_create_time = :#{#paramSearchDto.processCreateTime}) " +
            "and (:#{#paramSearchDto.processTitle} is null OR p.process_title LIKE CONCAT('%', :#{#paramSearchDto.processTitle}, '%')) " +
            "and (:#{#paramSearchDto.demandTitle} is null OR p.demand_title LIKE CONCAT('%', :#{#paramSearchDto.demandTitle}, '%')) " +
            "and (:#{#paramSearchDto.flowCreateOperatorName} is null OR p.flow_create_operator_name LIKE CONCAT('%', :#{#paramSearchDto.flowCreateOperatorName}, '%')) " +
            "and (:#{#paramSearchDto.flowCreateOperatorAccount} is null OR p.flow_create_operator_account LIKE CONCAT('%', :#{#paramSearchDto.flowCreateOperatorAccount}, '%')) " +
            "and (:#{#paramSearchDto.flowCreatorDepartment} is null OR p.flow_creator_department LIKE CONCAT('%', :#{#paramSearchDto.flowCreatorDepartment}, '%')) " +
            "and (:#{#paramSearchDto.systemName} is null OR p.system_name LIKE CONCAT('%', :#{#paramSearchDto.systemName}, '%')) " +
            "and (:#{#paramSearchDto.systemCode} is null OR p.system_code LIKE CONCAT('%', :#{#paramSearchDto.systemCode}, '%')) " +
            "and (coalesce(:#{#paramSearchDto.systemCodes}, null) is null or p.system_code in (:#{#paramSearchDto.systemCodes}))" +
            "and (:#{#paramSearchDto.systemAdminName} is null OR p.system_admin_name LIKE CONCAT('%', :#{#paramSearchDto.systemAdminName}, '%')) " +
            "and (:#{#paramSearchDto.systemAdminAccount} is null OR p.system_admin_account LIKE CONCAT('%', :#{#paramSearchDto.systemAdminAccount}, '%')) " +
            "and (:#{#paramSearchDto.systemBelongDepartment} is null OR p.system_belong_department LIKE CONCAT('%', :#{#paramSearchDto.systemBelongDepartment}, '%')) " +
            "and (:#{#paramSearchDto.demandClass} is null OR p.demand_class = :#{#paramSearchDto.demandClass}) " +
            "and (:#{#paramSearchDto.demandGrade} is null OR p.demand_grade = :#{#paramSearchDto.demandGrade}) " +
            "and (:#{#paramSearchDto.demandBackground} is null OR p.demand_background LIKE CONCAT('%', :#{#paramSearchDto.demandBackground}, '%')) " +
            "and (:#{#paramSearchDto.demandPurpose} is null OR p.demand_purpose LIKE CONCAT('%', :#{#paramSearchDto.demandPurpose}, '%')) " +
            "and (:#{#paramSearchDto.demandFrom} is null OR p.demand_from LIKE CONCAT('%', :#{#paramSearchDto.demandFrom}, '%')) " +
            "and (:#{#paramSearchDto.demandRelateName} is null OR p.demand_relate_name LIKE CONCAT('%', :#{#paramSearchDto.demandRelateName}, '%')) " +
            "and (:#{#paramSearchDto.demandRelateAccount} is null OR p.demand_relate_account LIKE CONCAT('%', :#{#paramSearchDto.demandRelateAccount}, '%')) " +
            "and (:#{#paramSearchDto.demandSummary} is null OR p.demand_summary LIKE CONCAT('%', :#{#paramSearchDto.demandSummary}, '%')) " +
            "and (:#{#paramSearchDto.demandBoundary} is null OR p.demand_boundary LIKE CONCAT('%', :#{#paramSearchDto.demandBoundary}, '%')) " +
            "and (:#{#paramSearchDto.demandRisk} is null OR p.demand_risk LIKE CONCAT('%', :#{#paramSearchDto.demandRisk}, '%')) " +
            "and (:#{#paramSearchDto.demandDetail} is null OR p.demand_detail LIKE CONCAT('%', :#{#paramSearchDto.demandDetail}, '%')) " +
            "and (:#{#paramSearchDto.demandSetup} is null OR p.demand_setup LIKE CONCAT('%', :#{#paramSearchDto.demandSetup}, '%')) " +
            "and (:#{#paramSearchDto.demandSetupResult} is null OR p.demand_setup_result = :#{#paramSearchDto.demandSetupResult}) " +
            "and (:#{#paramSearchDto.beginDateLocalDate} is null OR p.process_create_time >= :#{#paramSearchDto.beginDateLocalDate} )  " +
            "and (:#{#paramSearchDto.endDateLocalDate} is null OR p.process_create_time <= :#{#paramSearchDto.endDateLocalDate} )  " +
            "and (:#{#paramSearchDto.isClose} is null OR p.is_close = :#{#paramSearchDto.isClose}) " +
            "and (:#{#paramSearchDto.currentNode} is null OR p.current_node = :#{#paramSearchDto.currentNode}) " +
            "and (:#{#paramSearchDto.taskAccount} is null OR p.process_id in (:#{#paramSearchDto.taskProcessId})) " +
            " and (COALESCE(:#{#authCondition.admin}, null) IS null " +
            "or " +
            " ( " +
            "p.process_id IN (:#{#authCondition.processIds}) " +
            "or " +
            " p.system_code in (:#{#authCondition.systemCodes}) " +
            "or p.flow_create_operator_account in (:staffs) " +
            "or p.system_admin_account in (:staffs)  " +
            "or p.demand_relate_account in (:staffs)  " +
            " ) " +
            " ) " +
            "order by p.process_create_time desc" ,nativeQuery = true)
    Page<FlowFormDevManage> searchByParam(Pageable pageable, FlowParamDevManageSearchDto paramSearchDto, FlowParamAuthDto authCondition, List<String> staffs);

    List<FlowFormDevManage> findAllByIsDeleteAndIsCloseAndCurrentNodePassedIn(Integer noDelete, int noClose, List<String> nodeNames);

    List<FlowFormDevManage> queryAllByIsCloseAndIsDeleteAndSystemCodeAndCurrentNodePassedInOrderByInsertTimeDesc(int noClose, Integer noDelete, String systemCode, List<String> nodeNames);

    List<FlowFormDevManage> findAllByIsDeleteAndProcessTitleLike(Integer noDelete, String requirementTitle);

    List<FlowFormDevManage> findAllByIsDeleteAndIsClose(Integer noDelete, int noClose);

    List<FlowFormDevManage> queryAllByIsCloseAndIsDeleteAndSystemCodeAndCurrentNodeInOrderByInsertTimeDesc(int noClose, Integer noDelete, String systemCode, List<String> nodeNames);

    List<FlowFormDevManage> findAllByIsDeleteAndIsCloseAndCurrentNodeIn(Integer noDelete, int noClose, List<String> nodeNames);

    @Modifying
    @Query("UPDATE FlowFormDevManage f SET f.currentNode = :currentNode, f.isClose = :isClose WHERE f.processId = :processId")
    void updateCurrentNodeAndIsClose(@Param("currentNode") String currentNode, @Param("isClose") Integer isClose, @Param("processId") String processId);

    @Modifying
    @Query("UPDATE FlowFormDevManage f SET f.currentNode = :currentNode WHERE f.processId = :processId")
    void updateCurrentNode(@Param("currentNode") String currentNode, @Param("processId") String processId);

    List<FlowFormDevManage> findAllByCurrentNodeAndIsClose(String node, int noClose);
}
